<?php
include("../models/entities/class.AccountEntity.php");
@session_start();
if(isset($_SESSION["Username"]))
{
include("../models/webconfig.php");
include("../models/class.connection.php");
include("../models/entities/class.AuthorityEntity.php");
include("../models/repositories/class.AuthorityRepository.php");
include("../models/entities/class.GroupEntity.php");
include("../models/repositories/class.GroupRepository.php");

    include("../models/repositories/class.GroupAuthorityRepository.php");
    if(isset($_SESSION['Account'])){
        $groupAuthorityRepository = new GroupAuthorityRepository();
        $account = $_SESSION['Account'];
        if(!$groupAuthorityRepository->CheckExistGroupAuthority($account->GroupID_i, 4))
            header("location:NoAccess.php");
    }
    else
    {
        header("location:NoAccess.php");
    }
    
$authority = new AuthorityRepository();
$groupRepository = new GroupRepository();
$result = $authority->GetAll();
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    <head>
        <meta content="text/html; charset=UTF-8" http-equiv="Content-Type">
        <title>Trang quản trị WebFlower</title>
        <link href="../content/Layout.css" media="screen" rel="stylesheet" type="text/css">
        <link href="../content/groupManager.css" rel="stylesheet" type="text/css">
        <script type="text/javascript" src="../scripts/admingroup.js"></script>
        <script type="text/javascript" src="../scripts/jquery-1.4.2.min.js"></script>
        <script type="text/javascript">
            $(document).ready(function () {

                var str = location.href.toLowerCase();

                $('ul#mainNav li a').each(function () {
                    if (str.indexOf($(this).attr("href").toLowerCase()) > -1)
                        $(this).addClass("active");
                });
        

                $('ul#sideNav li a').each(function () {
                    if (str.indexOf($(this).attr("href").toLowerCase()) > -1)
                        $(this).addClass("active");
                });

                InsertPageScript.init();
            });
        </script>
    </head>

    <body>
        <form action="" method="post">
            <div id="format">
                <div id="wrapper">
                    <!--Phần main menu--><?php
include("../includes/backend_mainnav.php");
?>
                    <div id="containerHolder">
                        <div id="container">
                            <div id="maincontent">
                                <h3>Tạo nhóm người dùng</h3>
                                <div id="content">
                                    <p style="color:red; padding-left:20px;">
                                        <?php
                                        $check = array();
                                        if (isset($_POST['group_name'])) {
                                            if ($_POST['group_name'] != "") {
                                                if ($groupRepository->CheckGroupNameExist($_POST['group_name'])) {
                                                    if (isset($_POST['check'])) {
                                                        $check = $_POST['check'];
                                                        if (sizeof($check) > 0) {
                                                            if ($groupRepository->CreateGroup($_POST['group_name'], $check)) {
                                                                echo "(* Tạo nhóm thành công)";
                                                            } else {
                                                                echo "(* Tạo nhóm không thành công) Vui lòng thử lại!";
                                                            }
                                                        }
                                                    }else
                                                        echo "(* Bạn chưa chọn quyền cho nhóm)";
                                                }else
                                                    echo "(* Tên nhóm đã tồn tại)";
                                            }else {
                                                echo "(* Bạn chưa nhập tên nhóm)";
                                            }
                                        }
                                        ?>
                                    </p>
                                    <h4>Tên nhóm </h4><p><input name="group_name" id="group_name" value="<?php
                                        if (isset($_POST['group_name'])) {
                                            echo $_POST['group_name'];
                                        }
                                        ?>" type="text"> <label style="line-height:22px; color:red;">(*)</label></p>
                                    <h4>Chọn chức năng tương ứng </h4>
                                    <div id="function">
                                        <table width="100%">
                                            <tr class="animate">
                                                <td class="title">STT</td>
                                                <td class="title">Mã chức năng</td>
                                                <td class="title">Tên chức năng </td>
                                                <td class="title">Ghi chú</td>
                                            </tr>
                                            <?php if (isset($result)) {
                                            ?>
                                            <?php if (count($result) > 0) { ?>
<?php for ($i = 0; $i < count($result); $i++) { ?>
                                                        <tr class="animate">
                                                            <td><?php echo($i + 1); ?></td>
                                                            <td><?php echo($result[$i]->ID_i); ?></td>
                                                            <td style="text-align:left; padding-left:50px;">
                                                                <label><?php echo($result[$i]->Name_s); ?></label>
                                                                <input name="check[]" type="checkbox" value="<?php echo($result[$i]->ID_i); ?>"></td>
                                                            <td><?php echo($result[$i]->Description_s); ?></td>
                                                        </tr>
                                            <?php } ?>
                                            <?php } ?>
<?php } ?>
                                        </table>
                                        <p style="font-weight:bold; color:#54A7D8;">Chọn tất cả
                                            <input type="checkbox" name="checkall" id="checkall"></p>
                                    </div>
                                    <p style="margin-top:20px; text-align:center">
                                        <input id="submit" type="submit" value="Tạo mới" style="width: 126px">
                                    </p>
                                </div>
                            </div>
                        </div>
                        <!-- // #main --></div>
                    <!-- // #container -->
                    <!--Phần footer của trang--><?php
                                            include("../includes/backend_footer.php");
?></div>
            </div>
        </form>
    </body>

</html>
<?php
}
else
{
    header("location:../views/Admin_login.php?logout=TRUE");
}
?>